package com.aerotechnic.dao.implementation;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.JalonBiblioDaoIn;
import com.aerotechnic.entities.JalonBiblio;
import com.aerotechnic.entities.MacroTache;
import com.aerotechnic.entities.MacroTacheBiblio;
import com.aerotechnic.entities.User;

@Repository("JalonBiblioDao")
public class JalonBiblioDao implements JalonBiblioDaoIn {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(JalonBiblio t) {

		// TODO Auto-generated method stub

		getCurrentSession().save(t);

	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub

		JalonBiblio e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);

	}

	@Override
	public JalonBiblio findByID(Long id) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.getCurrentSession();

		List<JalonBiblio> jalonArray = session.createQuery(
				"FROM JalonBiblio where id= " + id).list();
		return jalonArray.isEmpty() ? null : jalonArray.get(0);

	}

	@Override
	public void update(JalonBiblio t) {
		// TODO Auto-generated method stub

		getCurrentSession().update(t);

	}

	public List<JalonBiblio> findAll() {
		// TODO Auto-generated method stub
		return filerJalons(getCurrentSession().createQuery("from JalonBiblio")
				.list());
	}

	@Override
	public int countAllJalon() {
		// TODO Auto-generated method stub
		List allJalon = filerJalons(getCurrentSession().createQuery(
				"from JalonBiblio ").list());
		return allJalon == null ? 0 : allJalon.size();      
	}

	@Override
	public List<JalonBiblio> find(int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		List<JalonBiblio> fetchedJalons = getCurrentSession()
				.createQuery("from JalonBiblio").setFirstResult(startIndex)
				.setMaxResults(pageSize).list();
		List<JalonBiblio> jalonList2return = new ArrayList<JalonBiblio>();
		for (int i = 0; i < fetchedJalons.size(); i++) {
			if (fetchedJalons.get(i).getClass() == JalonBiblio.class) {
				jalonList2return.add(fetchedJalons.get(i));
			}
		}
		return jalonList2return;
	}

	@Override
	public List<JalonBiblio> getJalonBiblioByIds(String[] ids) {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery(
				"from JalonBiblio where id in " + prepareIdsForQuery(ids))
				.list();
	}

	// helper methods

	private String prepareIdsForQuery(String[] ids) {
		String structuredIds = "(" + ids[0];
		for (int i = 1; i < ids.length - 1; i++) {
			structuredIds += " , " + ids[i];
		}
		structuredIds += " , " + ids[ids.length - 1] + " ) ";
		return structuredIds;
	}

	private List<JalonBiblio> filerJalons(List<JalonBiblio> arr2Filter) {
		List<JalonBiblio> jalonList2return = new ArrayList<JalonBiblio>();
		for (int i = 0; i < arr2Filter.size(); i++) {
			if (arr2Filter.get(i).getClass() == JalonBiblio.class) {
				jalonList2return.add(arr2Filter.get(i));
			}
		}
		return jalonList2return;
	}

}
